package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.line.service.TLineService;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.DictUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroup;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroupAudit;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentgroupAuditService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentgroupService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentproductService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;

/**
 * 整车发布审核Controller
 * @author dp
 * @version 2017-01-03
 */
@Controller
@RequestMapping(value = "${adminPath}/rentproduct/tRentgroupAudit")
public class TRentgroupAuditController extends BaseController {

	@Autowired
	private TRentgroupAuditService tRentgroupAuditService;
	@Autowired
	private TRentproductService tRentproductService;
	@Autowired
	private TRentgroupService tRentgroupService;
	@Autowired
	private TCargotypeService tCargotypeService;
	@Autowired
	private TSitedtlService tSitedtlService;
	@Autowired
	private TLineService tLineService;
	
	@ModelAttribute
	public TRentgroupAudit get(@RequestParam(required=false) String rentauditid) {
		TRentgroupAudit entity = null;
		if (StringUtils.isNotBlank(rentauditid)){
			entity = tRentgroupAuditService.get(rentauditid);
		}
		if (entity == null){
			entity = new TRentgroupAudit();
		}
		return entity;
	}
	
	@RequiresPermissions("rentproduct:tRentgroupAudit:view")
	@RequestMapping(value = {"list", ""})
	public String list(TRentgroupAudit tRentgroupAudit, String renttype, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TRentgroupAudit> page = tRentgroupAuditService.findPage(new Page<TRentgroupAudit>(request, response), tRentgroupAudit); 
		model.addAttribute("page", page);
		model.addAttribute("tRentgroupAudit", tRentgroupAudit);
		model.addAttribute("renttype", renttype);
		return "modules/rentproduct/tRentgroupAuditList";
	}

	@RequiresPermissions("rentproduct:tRentgroupAudit:view")
	@RequestMapping(value = "form")
	public String form(TRentgroupAudit tRentgroupAudit, Model model) {
		//读取包车信息
		if (tRentgroupAudit.getRentgroup() != null && tRentgroupAudit.getRentgroup().getRentgrpid() != null){
			TRentgroup tmp = new TRentgroup();
			tmp = tRentgroupService.get(tRentgroupAudit.getRentgroup().getRentgrpid());

			TRentproduct qry = new TRentproduct();
			qry.setRentgrpid(tmp.getRentgrpid());
			tmp.setProductList(tRentproductService.findList(qry));
			
			tRentgroupAudit.setRentgroup(tmp);
		}
		model.addAttribute("tRentgroupAudit", tRentgroupAudit);
		return "modules/rentproduct/tRentgroupAuditForm";
	}
	
	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "reason")
	public String reason(TRentgroupAudit tRentgroupAudit, Model model) {
		List<TRentgroupAudit> list = tRentgroupAuditService.findList(tRentgroupAudit);
		if(list!=null && !list.isEmpty()) {
			model.addAttribute("tRentgroupAudit", list.get(0));
		}
		return "modules/rentproduct/trentproductAuditReason";
	}
	
	@RequiresPermissions("rentproduct:tRentgroupAudit:view")
	@RequestMapping(value = "view")
	public String view(TRentgroup tRentgroup, Model model) {
		tRentgroup = tRentgroupService.get(tRentgroup.getRentgrpid().toString());
		String rentType = tRentgroup.getRenttype();
		// 按天包车
		if(rentType.equals(TRentgroup.RENT_TYPE_DAY)) {
			// 站点数据
			TSitedtl site = new TSitedtl();
			model.addAttribute("siteList", tSitedtlService.findList(site));
			
			// 车型数据
			List<TCargotype> cargotypeList = tCargotypeService.findList(new TCargotype());
			model.addAttribute("cargotypeList", cargotypeList);
		}
		// 旅游包车、接送飞机、接送火车
		if(rentType.equals(TRentgroup.RENT_TYPE_AIRPORT) ||
				rentType.equals(TRentgroup.RENT_TYPE_TRAIN) ||
				rentType.equals(TRentgroup.RENT_TYPE_TRAVEL)) {
			// 线路数据
			List<TLine> lineList = tLineService.findList(new TLine());
			model.addAttribute("lineList", lineList);
		}
		
		model.addAttribute("tRentgroup", tRentgroup);
		
		// 旅行商和电商数据
		List<Office> officeList = tRentgroupService.findOfficeList();
		model.addAttribute("officeList", officeList);
		return "modules/rentproduct/tRentgroupAuditView";
	}

	@RequiresPermissions("rentproduct:tRentgroupAudit:edit")
	@RequestMapping(value = "save")
	public String save(TRentgroupAudit tRentgroupAudit, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tRentgroupAudit)){
			return form(tRentgroupAudit, model);
		}
		tRentgroupAuditService.save(tRentgroupAudit);
		addMessage(redirectAttributes, "保存整车审核成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tRentgroup/rentforaudit?repage";
	}
	
	@RequiresPermissions("rentproduct:tRentgroupAudit:edit")
	@RequestMapping(value = "delete")
	public String delete(TRentgroupAudit tRentgroupAudit, RedirectAttributes redirectAttributes) {
		tRentgroupAuditService.delete(tRentgroupAudit);
		addMessage(redirectAttributes, "删除整车审核成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tRentgroupAudit/?repage";
	}

}